home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / libc / atoi.c < prev    next >
C/C++ Source or Header  |  1989-03-22  |  2KB  |  87 lines

  1. /* 
  2.  * atoi.c --
  3.  *
  4.  *    Source code for the "atoi" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/atoi.c,v 1.2 89/03/22 00:46:58 rab Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22.  
  23. /*
  24.  *----------------------------------------------------------------------
  25.  *
  26.  * atoi --
  27.  *
  28.  *    Convert an ASCII string into an integer.
  29.  *
  30.  * Results:
  31.  *    The return value is the integer equivalent of string.  If there
  32.  *    are no decimal digits in string, then 0 is returned.
  33.  *
  34.  * Side effects:
  35.  *    None.
  36.  *
  37.  *----------------------------------------------------------------------
  38.  */
  39.  
  40. int
  41. atoi(string)
  42.     register char *string;    /* String of ASCII digits, possibly
  43.                  * preceded by white space.  For bases
  44.                  * greater than 10, either lower- or
  45.                  * upper-case digits may be used.
  46.                  */
  47. {
  48.     register int result = 0;
  49.     register unsigned int digit;
  50.     int sign;
  51.  
  52.     /*
  53.      * Skip any leading blanks.
  54.      */
  55.  
  56.     while (isspace(*string)) {
  57.     string += 1;
  58.     }
  59.  
  60.     /*
  61.      * Check for a sign.
  62.      */
  63.  
  64.     if (*string == '-') {
  65.     sign = 1;
  66.     string += 1;
  67.     } else {
  68.     sign = 0;
  69.     if (*string == '+') {
  70.         string += 1;
  71.     }
  72.     }
  73.  
  74.     for ( ; ; string += 1) {
  75.     digit = *string - '0';
  76.     if (digit > 9) {
  77.         break;
  78.     }
  79.     result = (10*result) + digit;
  80.     }
  81.  
  82.     if (sign) {
  83.     return -result;
  84.     }
  85.     return result;
  86. }
  87.